package com.example.xgakj.mvp_demo.ui.activities.login;

import android.content.Context;

import com.example.xgakj.mvp_demo.App;
import com.example.xgakj.mvp_demo.entity.PostInfo;
import com.example.xgakj.mvp_demo.mvp.BasePresenterImpl;
import com.example.xgakj.mvp_demo.mvp.module.LoginInteraction;
import com.example.xgakj.mvp_demo.utils.Constant;
import com.example.xgakj.mvp_demo.utils.SharedPreferencesUtil;
import com.example.xgakj.mvp_demo.utils.ToastUtil;
import com.socks.library.KLog;

import io.reactivex.disposables.Disposable;


public class LoginPresenter extends BasePresenterImpl<LoginContract.View> implements LoginContract.Presenter, LoginInteraction.NetCallback {

    private Disposable mDisposable;
    private Context context;
    private LoginInteraction loginInteraction;
    String user,passWord;
    public LoginPresenter(){
        context= App.getAppContext();
        loginInteraction=new LoginInteraction(this);
    }

    @Override
    public void login(String user, String passWord) {
//"yuantong", "11111111111"
        loginInteraction.login(user, passWord);
        this.user=user;
        this.passWord=passWord;

    }

    @Override
    public void detachView() {
//        if (mDisposable!=null)
//        mDisposable.dispose();
        mView.dismissDialog();
        super.detachView();
    }

    @Override
    public void onSuccess(PostInfo value) {
        mView.dismissDialog();
        ToastUtil.showToast("登录成功");
        KLog.json(value.getMessage());
        SharedPreferencesUtil.setString(context, Constant.USER_NAME,user);
        SharedPreferencesUtil.setString(context, Constant.PASS_WORD,passWord);
        mView.enterMain();
    }

    @Override
    public void onError(Throwable e) {
        mView.dismissDialog();
        ToastUtil.showToast("登录失败");
    }

    @Override
    public void onSubscribe(Disposable d) {
        mView.showDialog();
        mDisposable=d;
    }
}
